﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.ComponentsService;
namespace eTMS.Controllers
{
    public class ComponentsControllerBase : CommonController
    {
        public ComponentsControllerBase()
            : base()
        {
            this.dao = new ComponentsServiceClient();
        }
        ~ComponentsControllerBase()
        {
            if (dao.State == System.ServiceModel.CommunicationState.Opened) try { dao.Close(); }
                catch { }
        }
        private DataTable _Table;
        private ComponentsEntity oComponents;
        public ComponentsEntity ComponentsObject
        {
            get
            {
                return oComponents;
            }
            set
            {
                oComponents = value;
            }
        }
        protected ComponentsServiceClient dao;
        public DataTable Table
        {
            get
            {
                this.LoadAllDSComponents(false);
                return _Table;
            }
        }
        public void LoadAllDSComponents(bool clearCache)
        {
            bool bForce = clearCache;
            if (_Table == null)
            {
                bForce = true;
            }
            if (!bForce) return;
            DataSet ds = dao.SelectAllDS();
            if (ds.Tables.Count > 0)
            {
                _Table = ds.Tables[0];
                this.AddIndexToTable(_Table);
            }
        }
        public void AddIndexToTable()
        {
            this.AddIndexToTable(_Table);
        }
        public void ResetTableIndex()
        {
            this.ResetTableIndex(_Table);
        }
        public bool AddNew()
        {
            if (oComponents == null) return false;
            bool result = true;
            try
            {
                oComponents = dao.SaveRet(oComponents);
            }
            catch
            {
                result = false;
            }
            if (result)
            {
                _ListComponents.Add(oComponents);
            }
            return result;
        }
        public bool Update()
        {
            if (oComponents == null) return false;
            return dao.Update(oComponents);
        }
        public bool Delete()
        {
            if (oComponents == null) return false;
            bool retvalue = dao.Delete(oComponents);
            if (retvalue)
            {
                _ListComponents.Remove(oComponents);
            }
            return retvalue;
        }
        public bool DeleteByID(Int32 Component_ID)
        {
            bool retvalue = dao.DeleteByID(Component_ID);
            if (retvalue)
            {
                ComponentsEntity oDeleted = FindComponents(Component_ID);
                if (oDeleted != null)
                {
                    _ListComponents.Remove(oDeleted);
                }
            }
            return retvalue;
        }
        public void ResetObject()
        {
            this.oComponents = new ComponentsEntity();
        }
        //Check if exists ComponentName
        public bool CheckExistsComponentName(object value)
        {
            return dao.SelectAllFilter("ComponentName=" + value.ToString()).Length > 0;
        }
        //Copy data from data row in grid to object
        public ComponentsEntity MapDataRowToObject(DataRow row)
        {
            ComponentsEntity o = new ComponentsEntity();
            o.ComponentName = Convert.ToString(row["ComponentName"]);
            o.SourceCode = Convert.ToString(row["SourceCode"]);
            if (!row.IsNull("Content"))
                o.Content = (Byte[])(row["Content"]);
            if (!row.IsNull("Component_ID"))
                o.Component_ID = (Int32)(row["Component_ID"]);
            return o;
        }
        //Copy data from object to data row in grid    
        public void MapObjectToDataRow(ComponentsEntity o, DataRow row)
        {
            row["ComponentName"] = o.ComponentName;
            row["SourceCode"] = o.SourceCode;
            row["Content"] = o.Content;
            row["Component_ID"] = o.Component_ID;
        }
        public void MapObjectToDataRow(DataRow row)
        {
            this.MapObjectToDataRow(this.ComponentsObject, row);
        }
        public void LoadObject(Int32 Component_ID)
        {
            ComponentsEntity oLoad = FindComponents(Component_ID);
            if (oLoad == null)
            {
                oComponents = this.LoadComponents(Component_ID);
                bool bNotIsNull = true;
                bNotIsNull &= oComponents.IsNullComponent_ID;
                if (!bNotIsNull)
                {
                    this.ListComponents.Add(oComponents);
                }
            }
            else
            {
                oComponents = oLoad;
            }
        }
        #region CachedList Components
        private static List<ComponentsEntity> _ListComponents = new List<ComponentsEntity>();
        public List<ComponentsEntity> ListComponents
        {
            get
            {
                return _ListComponents;
            }
        }
        public static List<ComponentsEntity> ListComponentsStatic
        {
            get
            {
                return _ListComponents;
            }
        }
        public ComponentsEntity FindComponents(Int32 Component_ID)
        {
            ComponentsEntity oResult = this.ListComponents.Find(delegate(ComponentsEntity oFind)
            {
                bool result = true;
                result &= oFind.Component_ID.Equals(Component_ID);
                return result;
            });
            return oResult;
        }
        public static void LoadAllComponents(bool clearCache)
        {
            if (_ListComponents.Count == 0)
                clearCache = true;
            if (!clearCache) return;
            ComponentsServiceClient dao = new ComponentsServiceClient();
            _ListComponents = new List<ComponentsEntity>(dao.SelectAll());
            if (dao.State == System.ServiceModel.CommunicationState.Opened) try { dao.Close(); }
                catch { }
        }
        public ComponentsEntity LoadComponents(Int32 Component_ID)
        {
            ComponentsEntity o = dao.SelectOne(Component_ID);
            return o;
        }
        #endregion
    }
}
